<!--
 * @Description: 
 * @Author: charles
 * @Date: 2021-11-08 11:47:22
 * @LastEditors: charles
 * @LastEditTime: 2021-11-09 10:24:49
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>列表渲染</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
  <script src="https://cdn.bootcss.com/babel-standalone/6.26.0/babel.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
</head>
<body>
  <div id="app"></div>
  <script type="text/babel">
  // 查询第一页资讯信息 并且显示到页面中
  const {useState,useEffect} = React;
  function App(props){
    // 状态
    let [resp,setResp] = useState({})
    const loadArticles = ()=>{
      let url = "http://121.199.29.84:8002/index/pageQueryArticles"
      let params = {page:1,pageSize:10}
      axios.get(url,{params}).then(( {data} ) =>{
        setResp(data)
      })
    }
    const delHandler = (id)=>{
      alert(id);
      // 删除
      
    }
    useEffect(() => {
      loadArticles();
      return ()=>{
        // 清理，闭包释放
      }
    }, ['resp']);

    let content ;
    if(resp.status === 200) {
      content = 
        <ul>
          {
            resp.data.list.map(item=>{
              return (
                <li key={item.id}>
                  {item.title}
                  <button onClick={()=>{
                    delHandler(item.id)
                  }}>删除</button>
                </li>
              )
            })
          }  
        </ul>
    } else {
      content = 
        <div>{resp.message}</div>
    }

    return (
      <div>
        <h2>资讯管理</h2>  
        <div>
          {content}  
        </div>
      </div>
    )
  }
  ReactDOM.render(<App/>, document.getElementById('app'));
  
  
  </script>
</body>
</html>

